import {timeFormat} from '../../model/utils/common';

export default {
    namespace: 'create',
    state: {
        detail : { },
        carModal : false,
        carList : null,
        price: {},
        location: {
            latitude: 0,
            longitude: 0
        }
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *init({ payload }, { put, call, select }){
            const id = payload && payload.id;
            // 获取车辆信息
            const {carList,carInfo} = yield put.resolve({
                type: 'car/getCarInfo'
            });
            let detail = {};
            if(!carInfo) return;
            // 展示详情
            if(id){
                detail = yield put.resolve({
                    type : '$request/post',
                    payload : {
                        url : '/api/detail',
                        data : {
                            id : payload.id
                        }
                    }
                });
                detail.oil_amount = detail.oilAmount;
                detail.total_price = detail.totalPrice;
                detail.oilDate = timeFormat(detail.oilTime, 'YYYY-MM-DD');
                detail.oilSecond = timeFormat(detail.oilTime, 'hh:mm');
            }else{
                detail.carNumber = carInfo.carNumber;
                detail.oilType = carInfo.oilType || 92;
                detail.oilDate = timeFormat(new Date(), 'YYYY-MM-DD');
                detail.oilSecond = timeFormat(new Date(), 'hh:mm');
            }

            yield put({
                type : 'save',
                payload : {
                    carInfo,
                    detail,
                    carList
                }
            });
            if(payload.callback){
                payload.callback();
            }
        },
        /**
         * 获取省份油价
         */
        *getOilPrice({ payload }, { put, call, select }){
            const {location, province} = payload;
            yield put({
                type: 'save',
                payload: { location }
            });
            let {detail, carInfo} = yield select(s => s.create);
            // 只查询今日油价
            const todayDate = timeFormat(new Date(), 'YYYY-MM-DD');
            // 油价
            const price = yield put.resolve({
                type: '$request/post',
                payload: {
                    url : '/api/getOilPrice',
                    data: {
                        province,
                        date : todayDate
                    }
                }
            });
            let newDetail = {
                ...detail,
                province,
                price : price['oil'+detail.oilType]/100,
                orgprice : price['oil'+detail.oilType]/100,
            };
            yield put({
                type: 'save',
                payload: {detail:newDetail}
            });
        },
        /**
         * 添加记录
         * @param {*} param0 
         * @param {*} param1 
         */
        *addOil({ payload }, { put, call, select }){
            const {detail, location} = yield select(state => state.create);
            const params = Object.assign({},detail);
            params.price = parseInt(detail.price*100);
            params.mileage = parseInt(detail.mileage);
            params.total_price = parseInt(detail.total_price*100);
            params.oil_amount = parseInt(detail.oil_amount);
            let oilTimeStr = `${detail.oilDate} ${detail.oilSecond}:00`;
            params.oil_time = new Date(oilTimeStr).getTime();
            if(!params.id){
                params.latitude = location.latitude;
                params.longitude = location.longitude;
            }
            const res = yield put.resolve({
                type : '$request/post',
                payload : {
                    url : '/api/create',
                    data : {
                        ...params,
                        oil_type : detail.oilType
                    }
                }
            });
            yield put.resolve({
                type : '$msg/showToast',
                payload : {
                    title : '保存成功',
                }
            });
            yield put({
                type : '$router/switchTab',
                payload : {
                    url : '/pages/oilList/index',
                }
            });
        },
        /**
         * 车牌号修改
         * @param {*} param0 
         * @param {*} param1 
         */
        * carNumberChange({ payload }, { put, call, select }){
            
            const {carList, detail,price} = yield select(s => s.create);
            const car = carList.find(item => item.carNumber == payload.carNumber);
            detail.price = price['oil'+car.oilType]/100;
            detail.oilType = car.oilType;
            detail.carNumber = payload.carNumber;
            yield put({
                type : 'save',
                payload : {
                    detail,
                    carModal: false
                }
            });
        }
    }
};